import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
import {
    getProductList,
    updateProductState,
    addProduct,
    queryProductDetails,
    verifyProduct,
    createProductAcquisition,
    getProductDisplay,
    getF2CProductList,
    getF2CProductDetail,
    openProduct,
    updateProductDetails,
} from '../services/api';

export default {
    namespace: 'product',

    state: {
        productList: [],
        retailsList: [],
        productLoading: false,
        productF2CLoading: false,
        productPage: {},
        productResult: {},
        productDetails: {},
        productSystemChannelList: [],
        productDisplay: null,
        f2CProductList: [],
        productF2CDetails: {},
    },

    effects: {
        *fetchProduct({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(getProductList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    productList: response.data.list,
                    productPage: response.data.page,
                }
            });
            if (callback) callback();
        },

        //获取产品开通列表
        *fetchgetF2CProductList({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productF2CLoading: true,
                },
            });
            const response = yield call(getF2CProductList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productF2CLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productF2CLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    f2CProductList: response.data,
                }
            });
            if (callback) callback();
        },

        //获取产品详情
        *queryProductDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(queryProductDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    productDetails: response.data,
                    //  retailsList: response.data.productDiviList,
                }
            });
            if (callback) callback();
        },

        //获取开通产品详情（F2C）
        *queryF2CProductDetail({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(getF2CProductDetail, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    productF2CDetails: response.data,
                }
            });
            if (callback) callback();
        },

        //产品开通
        *openProduct({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(openProduct, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                }
            });
            if (callback) callback();
        },

        //获取产品展业
        *getProductDisplay({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(getProductDisplay, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    productDisplay: response.data,
                }
            });
            if (callback) callback();
        },

        //添加产品
        *addProduct({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(addProduct, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                }
            });
            if (callback) callback();
        },

        //修改产品状态
        *updateProductState({ payload, callback }, { call, put }) {
            const response = yield call(updateProductState, payload);
            if (!isResponerCode(response)) {
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                }
            });
            if (callback) callback();
        },

        //产品详情修改
        *updateProductDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            const response = yield call(updateProductDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                },
            });
            if (callback) callback();
        },

        //产品审核
        *verifyProduct({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(verifyProduct, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                }
            });
            if (callback) callback();
        },

        //添加产品展业
        *createProductAcquisition({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    productLoading: true,
                },
            });
            const response = yield call(createProductAcquisition, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        productLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    productLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    productResult: response.result,
                }
            });
            if (callback) callback();
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        saveStepFormData(state, { payload }) {
            return {
                ...state,
                step: {
                    ...state.step,
                    ...payload,
                },
            };
        },
    },
};
